Lecture 20
benchd = tibble(
x = runif(10000),
y = runif(10000)
)
(b = bench::mark(
d[d$x > 0.5, ],
d[which(d$x > 0.5), ],
subset(d, x > 0.5),
filter(d, x > 0.5)
))# A tibble: 4 × 6
expression min median `itr/sec` mem_alloc `gc/sec`
<bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
1 d[d$x > 0.5, ] 49.4µs 56.7µs 17251. 238.11KB 46.9
2 d[which(d$x > 0.5), ] 90.7µs 103µs 9595. 268.38KB 48.3
3 subset(d, x > 0.5) 87.5µs 106.8µs 9234. 296.33KB 49.5
4 filter(d, x > 0.5) 287µs 314µs 3061. 1.48MB 18.4
d = tibble(
x = runif(1e6),
y = runif(1e6)
)
(b = bench::mark(
d[d$x > 0.5, ],
d[which(d$x > 0.5), ],
subset(d, x > 0.5),
filter(d, x > 0.5)
))# A tibble: 4 × 6
expression min median `itr/sec` mem_alloc `gc/sec`
<bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
1 d[d$x > 0.5, ] 3.62ms 4.08ms 247. 13.4MB 142.
2 d[which(d$x > 0.5), ] 8.12ms 8.93ms 111. 24.8MB 140.
3 subset(d, x > 0.5) 9.46ms 10.12ms 99.0 24.8MB 120.
4 filter(d, x > 0.5) 4.81ms 5.36ms 184. 24.8MB 272.
bench - relative results# A tibble: 4 × 6
expression min median `itr/sec` mem_alloc `gc/sec`
<bch:expr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 d[d$x > 0.5, ] 1 1 2.49 1 1.19
2 d[which(d$x > 0.5), ] 2.24 2.19 1.12 1.86 1.17
3 subset(d, x > 0.5) 2.62 2.48 1 1.86 1
4 filter(d, x > 0.5) 1.33 1.32 1.86 1.86 2.27
parallelPart of the base packages in R
tools for the forking of R processes (some functions do not work on Windows)
Core functions:
detectCores
pvec
mclapply
mcparallel & mccollect
detectCoresSurprisingly, detects the number of cores of the current system.
Parallelization of a vectorized function call
user system elapsed
0.089 0.012 0.100
user system elapsed
0.126 0.119 0.198
user system elapsed
0.092 0.165 0.168
bench::system_timecores = c(1,4,8,16)
order = 6:8
f = function(x,y) {
system.time(
pvec(1:(10^y), sqrt, mc.cores = x)
)[3]
}
res = map(
cores,
function(x) {
map_dbl(order, f, x = x)
}
) |>
do.call(rbind, args = _)
rownames(res) = paste0(cores," cores")
colnames(res) = paste0("10^",order)
res 10^6 10^7 10^8
1 cores 0.005 0.025 0.327
4 cores 0.041 0.161 1.687
8 cores 0.047 0.174 1.361
16 cores 0.055 0.185 1.359
Parallelized version of lapply
user system elapsed
0.267 0.006 0.275
user system elapsed
0.320 0.106 0.273
user system elapsed
0.326 0.100 0.179
user system elapsed
0.340 0.135 0.180
user system elapsed
0.354 0.159 0.204
Asynchronously evaluation of an R expression in a separate process
List of 2
$ pid: int 80672
$ fd : int [1:2] 4 7
- attr(*, "class")= chr [1:3] "parallelJob" "childProcess" "process"
List of 2
$ pid: int 80673
$ fd : int [1:2] 5 9
- attr(*, "class")= chr [1:3] "parallelJob" "childProcess" "process"
Checks mcparallel objects for completion
Packages by Revolution Analytics that provides the foreach function which is a parallelizable for loop (and then some).
Core functions:
registerDoMC
foreach, %dopar%, %do%
registerDoMCPrimarily used to set the number of cores used by foreach, by default uses options("cores") or half the number of cores found by detectCores from the parallel package.
foreachA slightly more powerful version of base for loops (think for with an lapply flavor). Combined with %do% or %dopar% for single or multicore execution.
foreach - iteratorsforeach can iterate across more than one value, but it doesn’t do length coercion
foreach - combining resultsforeach - parallelizationSwapping out %do% for %dopar% will use the parallel backend.
user system elapsed
0.296 0.027 0.110
user system elapsed
0.317 0.045 0.082
user system elapsed
0.329 0.047 0.074
user system elapsed
0.001 0.000 3.012
user system elapsed
0.050 0.007 3.083
Bootstrapping is a resampling scheme where the original data is repeatedly reconstructed by taking a samples of size n (with replacement) from the original data, and using that to repeat an analysis procedure of interest. Below is an example of fitting a local regression (loess) to some synthetic data, we will construct a bootstrap prediction interval for this model.
Optimal use of multiple cores is hard, there isn’t one best solution
Don’t underestimate the overhead cost
Experimentation is key
Measure it or it didn’t happen
Be aware of the trade off between developer time and run time
An awful lot of statistics is at its core linear algebra.
For example:
\[ \hat{\beta} = (X^T X)^{-1} X^Ty \]
Principle component analysis
Find \(T = XW\) where \(W\) is a matrix whose columns are the eigenvectors of \(X^TX\).
Often solved via SVD - Let \(X = U\Sigma W^T\) then \(T = U\Sigma\).
Not unique to Statistics, these are the type of problems that come up across all areas of numerical computing.
Numerical linear algebra \(\ne\) mathematical linear algebra
Efficiency and stability of numerical algorithms matter
Don’t reinvent the wheel - common core linear algebra tools (well defined API)
Low level algorithms for common linear algebra operations
Basic Linear Algebra Subprograms
Copying, scaling, multiplying vectors and matrices
Origins go back to 1979, written in Fortran
Linear Algebra Package
Higher level functionality building on BLAS.
Linear solvers, eigenvalues, and matrix decompositions
Origins go back to 1992, mostly Fortran (expanded on LINPACK, EISPACK)
Most default BLAS and LAPACK implementations (like R’s defaults) are somewhat dated
Written in Fortran and designed for a single cpu core
Certain (potentially non-optimal) hard coded defaults (e.g. block size).
Multithreaded alternatives:
ATLAS - Automatically Tuned Linear Algebra Software
OpenBLAS - fork of GotoBLAS from TACC at UTexas
Intel MKL - Math Kernel Library, part of Intel’s commercial compiler tools
cuBLAS / Magma - GPU libraries from Nvidia and UTK respectively
Accelerate / vecLib - Apple’s framework for GPU and multicore computing
| n | 1 core | 2 cores | 4 cores | 8 cores | 16 cores |
|---|---|---|---|---|---|
| 100 | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 |
| 500 | 0.004 | 0.003 | 0.002 | 0.002 | 0.004 |
| 1000 | 0.028 | 0.016 | 0.010 | 0.007 | 0.009 |
| 2000 | 0.207 | 0.110 | 0.058 | 0.035 | 0.039 |
| 3000 | 0.679 | 0.352 | 0.183 | 0.103 | 0.081 |
| 4000 | 1.587 | 0.816 | 0.418 | 0.227 | 0.145 |
| 5000 | 3.104 | 1.583 | 0.807 | 0.453 | 0.266 |
Sta 523 - Fall 2023